<?php
	require_once('../../globals.php');

	$notlogin=true;
	require_once('inc/weixiconfig.php');

	$code=$_GET['code'];
	$callback=$_GET['callback'];
	
	if(!empty($code)){
		$token=checkuseraccesstoken($code,0,1);
		if(!empty($token)){
			$userinfo=getuserinfo($token);
			echo $userinfo['subscribe'];
			if($userinfo['subscribe']==0){
				header('Location:https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI0MDk0ODIxMg==#wechat_redirect');exit;
			}else{
				if(!empty($userinfo)){
					$data=wechatlogin($userinfo);
					if(!empty($data['error'])){
						echo $data['error'];exit;
					}
					if(strpos($callback,'?')==false){
						$callback.='?';
					}else{
						$callback.='&';
					}
					$callback.='session_id='.$data['data']['session_id'];
				}
			}
		}
	}
	goback($callback);


	function goback($callback){
		if(empty($callback)){
			$backurl='/index.php';
		}else{
			$backurl=$callback;
		}
		
		header('Location:'.$backurl);
	}
?>
